package com.youkeda.app.control;


import com.youkeda.app.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName EmailCodeAPI
 * @Author 刘正星
 * @Date 2020/8/18 18:47
 **/
@Controller
@RequestMapping("/email/code")
public class EmailCodeAPI {
    public static final Logger LOG = LoggerFactory.getLogger(EmailCodeAPI.class);
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${sendEmailAddress}")
    private String sendEmailAddress;

    private static String EMAIL_EMPTY = "100001";

    @RequestMapping(path = "/send")
    @ResponseBody
    public Result<SimpleMailMessage> sendMessage(@RequestParam("email") String email){
        Result result = new Result();
        SimpleMailMessage message = new SimpleMailMessage();
        result.setSuccess(true);
        if (StringUtils.isEmpty(email)){
            result.setSuccess(false);
            result.setCode(EMAIL_EMPTY);
            result.setMessage("发送地址不能为空！");
            LOG.error("发送地址不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(sendEmailAddress)){
            result.setCode(EMAIL_EMPTY);
            result.setMessage("发件人地址不能为空");
            LOG.error("发件人地址不能为空");
            return result;
        }
        String code = String.valueOf((int)((Math.random()*9+1)*100000));

        redisTemplate.opsForValue().set(sendEmailAddress,code);
        message.setFrom(sendEmailAddress);
        message.setTo(sendEmailAddress);
        message.setSubject("发送验证码");
        message.setText(code);
        javaMailSender.send(message);
        result.setData(message);
        return result;
    }
    @RequestMapping(path = "/verificate")
    @ResponseBody
    public Result verificateCode(@RequestParam("email") String email, @RequestParam("code") String code) {

        Result result = new Result();
        result.setSuccess(false);
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(code)){
            result.setMessage("邮箱和验证码不能为空！");
            result.setCode("100001");
            return  result;
        }

        String exitCode = redisTemplate.opsForValue().get(sendEmailAddress);
        System.out.println(exitCode);
        if (StringUtils.isEmpty(exitCode)){
            result.setMessage("未取出验证码！");
            result.setCode("100002");
            return result;
        }
        if (!exitCode.equals(code)){
            result.setMessage("验证失败！");
            return result;
        }

        result.setMessage("验证成功！");
       result.setSuccess(true);
        return result;


    }
}
